/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.widgets;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

import org.openeccos.EccosDesktop;
import org.openeccos.model.MUser;
import org.openeccos.util.PDFormat;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.text.Document;
import nextapp.echo2.app.text.StringDocument;

/**
 * @author Alejandro Salas <br>
 * Created on Feb 5, 2007
 */
public class PDNumericField extends PDTextField {

	private double number;
	private NumberFormat numberFormat;

	public PDNumericField() {
		this(new StringDocument());
	}

	public PDNumericField(NumberFormat numberFormat) {
		this(new StringDocument());
		this.numberFormat = numberFormat;
	}

	/**
	 * Creates a new <code>NumberEditor</code> with the specified
	 * <code>Document</code> model.
	 *
	 * @param document
	 *            the document
	 */
	public PDNumericField(Document document) {
		super(document);
		if (numberFormat == null) {
			numberFormat = PDFormat.getNumberFormat(2);
		}
		initEditor();
	}

	/**
	 * Creates a new <code>NumberEditor</code> with the specified
	 * <code>Document</code> model, initial text, and column width.
	 *
	 * @param document
	 *            the document
	 * @param text
	 *            the initial text (may be null)
	 * @param columns
	 *            the number of columns to display
	 */
	public PDNumericField(Document document, String text, int columns) {
		super(document, text, columns);
		initEditor();
	}

	/**
	 * Creates a new <code>NumberEditor</code> with the specified text in a
	 * <code>StringDocument</code> model.
	 *
	 * @param text
	 *            the text to be placed into a <code>StringDocument</code>
	 */
	public PDNumericField(String text) {
		super(new StringDocument());
		if (text != null) {
			setText(text);
		}
		initEditor();
	}

	/**
	 * Creates a new <code>NumberEditor</code> with the initial text, and
	 * column width.
	 *
	 * @param text
	 *            the initial text (may be null)
	 * @param columns
	 *            the number of columns to display
	 */
	public PDNumericField(String text, int columns) {
		this(new StringDocument(), text, columns);
	}

	/**
	 * Creates a new <code>NumberEditor</code> with the specified column
	 * width.
	 *
	 * @param columns
	 *            the number of columns to display
	 */
	public PDNumericField(int columns) {
		this(new StringDocument(), null, columns);
	}

	private void initEditor() {
		setAlignment(new Alignment(Alignment.RIGHT, Alignment.DEFAULT));
		setInsets(new Insets(3, 1, 3, 0));
	}

	@Override
	@Deprecated
	public void setText(String newValue) {
		try {
			Number n = numberFormat.parse(newValue);
			super.setText(numberFormat.format(n));
		} catch (Exception e) {
			super.setText("0");
		}
	}

	@Override
	@Deprecated
	public String getText() {
		return super.getText();
	}

	public void setNumber(double number) {
		this.number = number;
		super.setText(numberFormat.format(number));
	}

	public double getNumber() {
		try {
			number = numberFormat.parse(getText()).doubleValue();
		} catch (ParseException e) {
		}

		setNumber(number);
		return number;
	}

	public NumberFormat getNumberFormat() {
		return numberFormat;
	}

	public void setNumberFormat(NumberFormat numberFormat) {
		this.numberFormat = numberFormat;
	}
}